package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import base.DbConnect;
import base.Session;
import base.Sql;
import model.Manager;
import model.Student;

public class StudentDao {
	public static Student getStudent(String username) {
		Connection conn = DbConnect.getConnect();
		Statement stmt = null;
		ResultSet rs = null;
		model.Student student = new model.Student();
		try {
			stmt = conn.createStatement();
			String sql;
			sql = "SELECT * FROM student where stu_id = '" + username + "'";
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				student.setStu_id(rs.getString("stu_id"));
				student.setStu_name(rs.getString("stu_name"));
				student.setStu_sex(rs.getString("stu_sex"));
				student.setStu_birthday(rs.getString("stu_birthday"));
				student.setStu_sfz(rs.getString("stu_sfz"));
				student.setStu_tel(rs.getString("stu_tel"));
				student.setStu_province(rs.getString("stu_province"));
				student.setStu_email(rs.getString("stu_email"));
				student.setPassword(rs.getString("password"));
				student.setNation(rs.getString("nation"));
				student.setPolitical(rs.getString("political"));
				student.setPolitical(rs.getString("m_id"));
				student.setEnrollmentYear(rs.getInt("enrollmentYear"));
				student.setCla_id(rs.getString("cla_id"));
			}
		} catch (SQLException se) {
			se.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
				conn.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return student;
	}
	
	
	public static int insert(Student student) {
		Connection conn = DbConnect.getConnect();
		PreparedStatement stmt = null;
		int res = -1;
		String sql = "insert into student values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try {
			stmt = (PreparedStatement) conn.prepareStatement(sql);
			stmt.setString(1, student.getStu_id());
			stmt.setString(2, student.getStu_name());
			stmt.setString(3, student.getStu_sex());
			stmt.setString(4, student.getStu_birthday());
			stmt.setString(5, student.getStu_sfz());
			stmt.setString(6, student.getStu_tel());
			stmt.setString(7, student.getStu_province());
			stmt.setString(8, student.getStu_email());
			stmt.setString(9, student.getPassword());
			stmt.setString(10, student.getNation());
			stmt.setString(11, student.getPolitical());
			stmt.setString(12, student.getM_id());
			stmt.setInt(13, student.getEnrollmentYear());
			stmt.setString(14, student.getCla_id());
			res = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}
	
	

	public static int updata(Student student) {
		Connection conn = DbConnect.getConnect();		
		PreparedStatement stmt = null;
		int res = -1;
		try {
			String sql = "update student set stu_id = ? ,stu_name = ? ,stu_sex = ? ,stu_birthday = ? ,stu_sfz = ? ,stu_tel = ? ,stu_province = ? ,stu_email = ? ,password = ? ,nation = ? ,political = ? ,m_id = ? , enrollmentYear = ?, cla_id = ? where stu_id = ?";
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, student.getStu_id());
			stmt.setString(2, student.getStu_name());
			stmt.setString(3, student.getStu_sex());
			stmt.setString(4, student.getStu_birthday());
			stmt.setString(5, student.getStu_sfz());
			stmt.setString(6, student.getStu_tel());
			stmt.setString(7, student.getStu_province());
			stmt.setString(8, student.getStu_email());
			stmt.setString(9, student.getPassword());
			stmt.setString(10, student.getNation());
			stmt.setString(11, student.getPolitical());
			stmt.setString(12, student.getM_id());
			stmt.setInt(13, student.getEnrollmentYear());
			stmt.setString(14, student.getCla_id());
			stmt.setString(15, student.getStu_id());
			res = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return res;
	}
	
	public static ArrayList<Student> select() {
		Connection conn = DbConnect.getConnect();
		Statement stmt = null;
		ResultSet rs = null;
		ArrayList<Student> arrayList = new ArrayList<Student>();
		
		String sql;
		sql = "SELECT * FROM student";
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				Student student = new Student();
				student.setStu_id(rs.getString("stu_id"));
				student.setStu_name(rs.getString("stu_name"));
				student.setStu_sex(rs.getString("stu_sex"));
				student.setStu_birthday(rs.getString("stu_birthday"));
				student.setStu_sfz(rs.getString("stu_sfz"));
				student.setStu_tel(rs.getString("stu_tel"));
				student.setStu_province(rs.getString("stu_province"));
				student.setStu_email(rs.getString("stu_email"));
				student.setPassword(rs.getString("password"));
				student.setNation(rs.getString("nation"));
				student.setPolitical(rs.getString("political"));
				student.setM_id(rs.getString("m_id"));
				student.setEnrollmentYear(rs.getInt("enrollmentYear"));
				student.setCla_id(rs.getString("cla_id"));
				arrayList.add(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return arrayList;
	}
	
	public static Student select(String stu_id) {
		Connection conn = DbConnect.getConnect();
		Statement stmt = null;
		ResultSet rs = null;
		Student student = new Student();
		String sql;
		sql = "SELECT * FROM student where stu_id = '" + stu_id + "'";
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				student.setStu_id(rs.getString("stu_id"));
				student.setStu_name(rs.getString("stu_name"));
				student.setStu_sex(rs.getString("stu_sex"));
				student.setStu_birthday(rs.getString("stu_birthday"));
				student.setStu_sfz(rs.getString("stu_sfz"));
				student.setStu_tel(rs.getString("stu_tel"));
				student.setStu_province(rs.getString("stu_province"));
				student.setStu_email(rs.getString("stu_email"));
				student.setPassword(rs.getString("password"));
				student.setNation(rs.getString("nation"));
				student.setPolitical(rs.getString("political"));
				student.setM_id(rs.getString("m_id"));
				student.setEnrollmentYear(rs.getInt("enrollmentYear"));
				student.setCla_id(rs.getString("cla_id"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return student;
	}
	
	public static int delete(String stu_id) {
		Connection conn = DbConnect.getConnect();
		PreparedStatement stmt = null;
		int res = -1;
		String sql = "delete from student where stu_id = ?";
		try {
			stmt = (PreparedStatement) conn.prepareStatement(sql);
			stmt.setString(1, stu_id);
			res = stmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			conn.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return res;
	}

}
